home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / misc / skey-2.000 / skey-2 / skey-2.2 / login / login.c < prev    next >
C/C++ Source or Header  |  1995-04-02  |  24KB  |  1,023 lines

  1. /* This program is derived from 4.3 BSD software and is
  2.    subject to the copyright notice below.
  3.  
  4.    The port to HP-UX has been motivated by the incapability
  5.    of 'rlogin'/'rlogind' as per HP-UX 6.5 (and 7.0) to transfer window sizes.
  6.  
  7.    Changes:
  8.  
  9.    - General HP-UX portation. Use of facilities not available
  10.      in HP-UX (e.g. setpriority) has been eliminated.
  11.      Utmp/wtmp handling has been ported.
  12.  
  13.    - The program uses BSD command line options to be used
  14.      in connection with e.g. 'rlogind' i.e. 'new login'.
  15.  
  16.    - HP features left out:          logging of bad login attempts in /etc/btmp,
  17.                     they are sent to syslog
  18.  
  19.                     password expiry
  20.  
  21.                     '*' as login shell, add it if you need it
  22.  
  23.    - BSD features left out:         quota checks
  24.                     password expiry
  25.                     analysis of terminal type (tset feature)
  26.  
  27.    - BSD features thrown in:        Security logging to syslogd.
  28.                                     This requires you to have a (ported) syslog
  29.                     system -- 7.0 comes with syslog
  30.                     
  31.                     'Lastlog' feature.
  32.  
  33.    - A lot of nitty gritty details has been adjusted in favour of
  34.      HP-UX, e.g. /etc/securetty, default paths and the environment
  35.      variables assigned by 'login'.
  36.  
  37.    - We do *nothing* to setup/alter tty state, under HP-UX this is
  38.      to be done by getty/rlogind/telnetd/some one else.
  39.  
  40.    Michael Glad (glad@daimi.dk)
  41.    Computer Science Department
  42.    Aarhus University
  43.    Denmark
  44.  
  45.    1990-07-04
  46.  
  47.    1991-09-24 glad@daimi.aau.dk: HP-UX 8.0 port:
  48.               - now explictly sets non-blocking mode on descriptors
  49.           - strcasecmp is now part of HP-UX
  50.    1992-02-05 poe@daimi.aau.dk: Ported the stuff to Linux 0.12
  51.    From 1992 till now (1995) this code for Linux has been maintained at
  52.    ftp.daimi.aau.dk:/pub/linux/poe/
  53. */
  54.    
  55. /*
  56.  * Copyright (c) 1980, 1987, 1988 The Regents of the University of California.
  57.  * All rights reserved.
  58.  *
  59.  * Redistribution and use in source and binary forms are permitted
  60.  * provided that the above copyright notice and this paragraph are
  61.  * duplicated in all such forms and that any documentation,
  62.  * advertising materials, and other materials related to such
  63.  * distribution and use acknowledge that the software was developed
  64.  * by the University of California, Berkeley.  The name of the
  65.  * University may not be used to endorse or promote products derived
  66.  * from this software without specific prior written permission.
  67.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  68.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  69.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  70.  */
  71.  
  72. #ifndef lint
  73. char copyright[] =
  74. "@(#) Copyright (c) 1980, 1987, 1988 The Regents of the University of California.\n\
  75.  All rights reserved.\n";
  76. #endif /* not lint */
  77.  
  78. #ifndef lint
  79. static char sccsid[] = "@(#)login.c    5.40 (Berkeley) 5/9/89";
  80. #endif /* not lint */
  81.  
  82. /*
  83.  * login [ name ]
  84.  * login -h hostname    (for telnetd, etc.)
  85.  * login -f name    (for pre-authenticated login: datakit, xterm, etc.)
  86.  */
  87.  
  88. /* #define TESTING */
  89.  
  90. #ifdef TESTING
  91. #include "param.h"
  92. #else
  93. #include <sys/param.h>
  94. #endif
  95.  
  96. #include <ctype.h>
  97. #include <unistd.h>
  98. #include <getopt.h>
  99. #include <memory.h>
  100. #include <sys/stat.h>
  101. #include <sys/time.h>
  102. #include <sys/resource.h>
  103. #include <sys/file.h>
  104. #include <termios.h>
  105. #include <string.h>
  106. #define index strchr
  107. #define rindex strrchr
  108. #include <sys/ioctl.h>
  109. #include <signal.h>
  110. #include <errno.h>
  111. #include <grp.h>
  112. #include <pwd.h>
  113. #include <setjmp.h>
  114. #include <stdlib.h>
  115. #include <stdio.h>
  116. #include <string.h>
  117. #include <sys/syslog.h>
  118. #include <sys/sysmacros.h>
  119. #include <netdb.h>
  120.  
  121. #ifdef SKEY
  122. #  include <skey.h>
  123. #endif
  124.  
  125. #ifdef TESTING
  126. #  include "utmp.h"
  127. #else
  128. #  include <utmp.h>
  129. #endif
  130.  
  131. #ifdef SHADOW_PWD
  132. #include <shadow.h>
  133. #endif
  134.  
  135. #ifndef linux
  136. #include <tzfile.h>
  137. #include <lastlog.h>
  138. #else
  139. struct  lastlog
  140.   { long ll_time;
  141.     char ll_line[12];
  142.     char ll_host[16];
  143.   };
  144. #endif
  145.  
  146. #include "pathnames.h"
  147.  
  148. #define P_(s) ()
  149. void opentty P_((const char *tty));
  150. void getloginname P_((void));
  151. void timedout P_((void));
  152. int rootterm P_((char *ttyn));
  153. void motd P_((void));
  154. void sigint P_((void));
  155. void checknologin P_((void));
  156. void dolastlog P_((int quiet));
  157. void badlogin P_((char *name));
  158. char *stypeof P_((char *ttyid));
  159. void checktty P_((char *user, char *tty));
  160. void getstr P_((char *buf, int cnt, char *err));
  161. void sleepexit P_((int eval));
  162. #undef P_
  163.  
  164. #ifdef    KERBEROS
  165. #include <kerberos/krb.h>
  166. #include <sys/termios.h>
  167. char    realm[REALM_SZ];
  168. int    kerror = KSUCCESS, notickets = 1;
  169. #endif
  170.  
  171. #ifndef linux
  172. #define    TTYGRPNAME    "tty"        /* name of group to own ttys */
  173. #else
  174. #  define TTYGRPNAME      "other"
  175. #  ifndef MAXPATHLEN
  176. #    define MAXPATHLEN 1024
  177. #  endif
  178. #endif
  179.  
  180. /*
  181.  * This bounds the time given to login.  Not a define so it can
  182.  * be patched on machines where it's too small.
  183.  */
  184. #ifndef linux
  185. int    timeout = 300;
  186. #else
  187. int     timeout = 60;
  188. #endif
  189.  
  190. struct    passwd *pwd;
  191. int    failures;
  192. char    term[64], *hostname, *username, *tty;
  193.  
  194. char    thishost[100];
  195.  
  196. #ifndef linux
  197. struct    sgttyb sgttyb;
  198. struct    tchars tc = {
  199.     CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK
  200. };
  201. struct    ltchars ltc = {
  202.     CSUSP, CDSUSP, CRPRNT, CFLUSH, CWERASE, CLNEXT
  203. };
  204. #endif
  205.  
  206. char *months[] =
  207.     { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
  208.       "Sep", "Oct", "Nov", "Dec" };
  209.  
  210. /* provided by Linus Torvalds 16-Feb-93 */
  211. void 
  212. opentty(const char * tty)
  213. {
  214.     int i;
  215.     int fd = open(tty, O_RDWR);
  216.  
  217.     for (i = 0 ; i < fd ; i++)
  218.       close(i);
  219.     for (i = 0 ; i < 3 ; i++)
  220.       dup2(fd, i);
  221.     if (fd >= 3)
  222.       close(fd);
  223. }
  224.  
  225. int
  226. main(argc, argv)
  227.     int argc;
  228.     char **argv;
  229. {
  230.     extern int errno, optind;
  231.     extern char *optarg, **environ;
  232.     struct timeval tp;
  233.     struct tm *ttp;
  234.     struct group *gr;
  235.     register int ch;
  236.     register char *p;
  237.     int ask, fflag, hflag, pflag, cnt;
  238.     int quietlog, passwd_req, ioctlval;
  239.     char *domain, *salt, *ttyn, *pp;
  240.     char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
  241.     char *ctime(), *ttyname(), *stypeof();
  242.     time_t time();
  243.     void timedout();
  244.     char *termenv;
  245. #ifdef SKEY
  246.     int permit_passwd = 0;
  247. #endif
  248.  
  249. #ifdef linux
  250.     char tmp[100];
  251.     /* Just as arbitrary as mountain time: */
  252.         /* (void)setenv("TZ", "MET-1DST",0); */
  253. #endif
  254.  
  255.     (void)signal(SIGALRM, timedout);
  256.     (void)alarm((unsigned int)timeout);
  257.     (void)signal(SIGQUIT, SIG_IGN);
  258.     (void)signal(SIGINT, SIG_IGN);
  259.  
  260.     (void)setpriority(PRIO_PROCESS, 0, 0);
  261. #ifdef HAVE_QUOTA
  262.     (void)quota(Q_SETUID, 0, 0, 0);
  263. #endif
  264.  
  265.     /*
  266.      * -p is used by getty to tell login not to destroy the environment
  267.       * -f is used to skip a second login authentication 
  268.      * -h is used by other servers to pass the name of the remote
  269.      *    host to login so that it may be placed in utmp and wtmp
  270.      */
  271.     (void)gethostname(tbuf, sizeof(tbuf));
  272.     (void)strncpy(thishost, tbuf, sizeof(thishost)-1);
  273.     domain = index(tbuf, '.');
  274.  
  275.     fflag = hflag = pflag = 0;
  276.     passwd_req = 1;
  277.     while ((ch = getopt(argc, argv, "fh:p")) != EOF)
  278.         switch (ch) {
  279.         case 'f':
  280.             fflag = 1;
  281.             break;
  282.  
  283.         case 'h':
  284.             if (getuid()) {
  285.                 (void)fprintf(stderr,
  286.                     "login: -h for super-user only.\n");
  287.                 exit(1);
  288.             }
  289.             hflag = 1;
  290.             if (domain && (p = index(optarg, '.')) &&
  291.                 strcasecmp(p, domain) == 0)
  292.                 *p = 0;
  293.             hostname = optarg;
  294.             break;
  295.  
  296.         case 'p':
  297.             pflag = 1;
  298.             break;
  299.         case '?':
  300.         default:
  301.             (void)fprintf(stderr,
  302.                 "usage: login [-fp] [username]\n");
  303.             exit(1);
  304.         }
  305.     argc -= optind;
  306.     argv += optind;
  307.     if (*argv) {
  308.         username = *argv;
  309.         ask = 0;
  310.     } else
  311.         ask = 1;
  312.  
  313. #ifndef linux
  314.     ioctlval = 0;
  315.     (void)ioctl(0, TIOCLSET, &ioctlval);
  316.     (void)ioctl(0, TIOCNXCL, 0);
  317.     (void)fcntl(0, F_SETFL, ioctlval);
  318.     (void)ioctl(0, TIOCGETP, &sgttyb);
  319.     sgttyb.sg_erase = CERASE;
  320.     sgttyb.sg_kill = CKILL;
  321.     (void)ioctl(0, TIOCSLTC, <c);
  322.     (void)ioctl(0, TIOCSETC, &tc);
  323.     (void)ioctl(0, TIOCSETP, &sgttyb);
  324.  
  325.     /*
  326.      * Be sure that we're in
  327.      * blocking mode!!!
  328.      * This is really for HPUX
  329.      */
  330.         ioctlval = 0;
  331.         (void)ioctl(0, FIOSNBIO, &ioctlval);
  332. #endif
  333.  
  334.     for (cnt = getdtablesize(); cnt > 2; cnt--)
  335.         close(cnt);
  336.  
  337.     ttyn = ttyname(0);
  338.     if (ttyn == NULL || *ttyn == '\0') {
  339.         (void)sprintf(tname, "%s??", _PATH_TTY);
  340.         ttyn = tname;
  341.     }
  342.  
  343.     setpgrp();
  344.  
  345.     {
  346.         struct termios tt, ttt;
  347.  
  348.         tcgetattr(0, &tt);
  349.         ttt = tt;
  350.         ttt.c_cflag &= ~HUPCL;
  351.  
  352.         if((chown(ttyn, 0, 0) == 0) && (chmod(ttyn, 0622) == 0)) {
  353.         tcsetattr(0,TCSAFLUSH,&ttt);
  354.         signal(SIGHUP, SIG_IGN); /* so vhangup() wont kill us */
  355.         vhangup();
  356.         signal(SIGHUP, SIG_DFL);
  357.         }
  358.  
  359.         setsid();
  360.  
  361.         /* re-open stdin,stdout,stderr after vhangup() closed them */
  362.         /* if it did, after 0.99.5 it doesn't! */
  363.         opentty(ttyn);
  364.         tcsetattr(0,TCSAFLUSH,&tt);
  365.     }
  366.  
  367.     if (tty = rindex(ttyn, '/'))
  368.         ++tty;
  369.     else
  370.         tty = ttyn;
  371.  
  372.     openlog("login", LOG_ODELAY, LOG_AUTH);
  373.  
  374.     for (cnt = 0;; ask = 1) {
  375.         ioctlval = 0;
  376. #ifndef linux
  377.         (void)ioctl(0, TIOCSETD, &ioctlval);
  378. #endif
  379.  
  380.         if (ask) {
  381.             fflag = 0;
  382.             getloginname();
  383.         }
  384.  
  385.         checktty(username, tty);
  386.  
  387.         (void)strcpy(tbuf, username);
  388.         if (pwd = getpwnam(username))
  389.             salt = pwd->pw_passwd;
  390.         else
  391.             salt = "xx";
  392.  
  393.         /* if user not super-user, check for disabled logins */
  394.         if (pwd == NULL || pwd->pw_uid)
  395.             checknologin();
  396.  
  397.         /*
  398.          * Disallow automatic login to root; if not invoked by
  399.          * root, disallow if the uid's differ.
  400.          */
  401.         if (fflag && pwd) {
  402.             int uid = getuid();
  403.  
  404.             passwd_req = pwd->pw_uid == 0 ||
  405.                 (uid && uid != pwd->pw_uid);
  406.         }
  407.  
  408.         /*
  409.          * If trying to log in as root, but with insecure terminal,
  410.          * refuse the login attempt.
  411.          */
  412.         if (pwd && pwd->pw_uid == 0 && !rootterm(tty)) {
  413.             (void)fprintf(stderr,
  414.                 "%s login refused on this terminal.\n",
  415.                 pwd->pw_name);
  416.  
  417.             if (hostname)
  418.                 syslog(LOG_NOTICE,
  419.                     "LOGIN %s REFUSED FROM %s ON TTY %s",
  420.                     pwd->pw_name, hostname, tty);
  421.             else
  422.                 syslog(LOG_NOTICE,
  423.                     "LOGIN %s REFUSED ON TTY %s",
  424.                      pwd->pw_name, tty);
  425.             continue;
  426.         }
  427.  
  428.         /*
  429.          * If no pre-authentication and a password exists
  430.          * for this user, prompt for one and verify it.
  431.          */
  432.         if (!passwd_req || (pwd && !*pwd->pw_passwd))
  433.             break;
  434.  
  435.         setpriority(PRIO_PROCESS, 0, -4);
  436. #ifdef SKEY
  437.         permit_passwd = skeyaccess(username, tty, 
  438.                        hostname ? hostname : NULL,
  439.                        NULL);
  440.         pp = skey_getpass("Password:", pwd, permit_passwd);
  441.         p = skey_crypt(pp, salt, pwd, permit_passwd);
  442. #else
  443.         pp = getpass("Password: ");
  444.         p = crypt(pp, salt);
  445. #endif
  446.         setpriority(PRIO_PROCESS, 0, 0);
  447.  
  448. #ifdef    KERBEROS
  449.  
  450.         /*
  451.          * If not present in pw file, act as we normally would.
  452.          * If we aren't Kerberos-authenticated, try the normal
  453.          * pw file for a password.  If that's ok, log the user
  454.          * in without issueing any tickets.
  455.          */
  456.  
  457.         if (pwd && !krb_get_lrealm(realm,1)) {
  458.             /*
  459.              * get TGT for local realm; be careful about uid's
  460.              * here for ticket file ownership
  461.              */
  462.             (void)setreuid(geteuid(),pwd->pw_uid);
  463.             kerror = krb_get_pw_in_tkt(pwd->pw_name, "", realm,
  464.                 "krbtgt", realm, DEFAULT_TKT_LIFE, pp);
  465.             (void)setuid(0);
  466.             if (kerror == INTK_OK) {
  467.                 memset(pp, 0, strlen(pp));
  468.                 notickets = 0;    /* user got ticket */
  469.                 break;
  470.             }
  471.         }
  472. #endif
  473.         (void) memset(pp, 0, strlen(pp));
  474.         if (pwd && !strcmp(p, pwd->pw_passwd))
  475.             break;
  476.  
  477.         (void)printf("Login incorrect\n");
  478.         failures++;
  479.         badlogin(username); /* log ALL bad logins */
  480.  
  481.         /* we allow 10 tries, but after 3 we start backing off */
  482.         if (++cnt > 3) {
  483.             if (cnt >= 10) {
  484.                 sleepexit(1);
  485.             }
  486.             sleep((unsigned int)((cnt - 3) * 5));
  487.         }
  488.     }
  489.  
  490.     /* committed to login -- turn off timeout */
  491.     (void)alarm((unsigned int)0);
  492.  
  493. #ifdef HAVE_QUOTA
  494.     if (quota(Q_SETUID, pwd->pw_uid, 0, 0) < 0 && errno != EINVAL) {
  495.         switch(errno) {
  496.         case EUSERS:
  497.             (void)fprintf(stderr,
  498.         "Too many users logged on already.\nTry again later.\n");
  499.             break;
  500.         case EPROCLIM:
  501.             (void)fprintf(stderr,
  502.                 "You have too many processes running.\n");
  503.             break;
  504.         default:
  505.             perror("quota (Q_SETUID)");
  506.         }
  507.         sleepexit(0);
  508.     }
  509. #endif
  510.  
  511.     /* paranoia... */
  512.     endpwent();
  513.  
  514.     /* This requires some explanation: As root we may not be able to
  515.        read the directory of the user if it is on an NFS mounted
  516.        filesystem. We temporarily set our effective uid to the user-uid
  517.        making sure that we keep root privs. in the real uid. 
  518.  
  519.        A portable solution would require a fork(), but we rely on Linux
  520.        having the BSD setreuid() */
  521.  
  522.     {
  523.         char tmpstr[MAXPATHLEN];
  524.         uid_t ruid = getuid();
  525.         gid_t egid = getegid();
  526.  
  527.         strncpy(tmpstr, pwd->pw_dir, MAXPATHLEN-12);
  528.         strncat(tmpstr, ("/" _PATH_HUSHLOGIN), MAXPATHLEN);
  529.  
  530.         setregid(-1, pwd->pw_gid);
  531.         setreuid(0, pwd->pw_uid);
  532.         quietlog = (access(tmpstr, R_OK) == 0);
  533.         setuid(0); /* setreuid doesn't do it alone! */
  534.         setreuid(ruid, 0);
  535.         setregid(-1, egid);
  536.     }
  537.  
  538. #ifndef linux
  539. #ifdef KERBEROS
  540.     if (notickets && !quietlog)
  541.         (void)printf("Warning: no Kerberos tickets issued\n");
  542. #endif
  543.  
  544. #define    TWOWEEKS    (14*24*60*60)
  545.     if (pwd->pw_change || pwd->pw_expire)
  546.         (void)gettimeofday(&tp, (struct timezone *)NULL);
  547.     if (pwd->pw_change)
  548.         if (tp.tv_sec >= pwd->pw_change) {
  549.             (void)printf("Sorry -- your password has expired.\n");
  550.             sleepexit(1);
  551.         }
  552.         else if (tp.tv_sec - pwd->pw_change < TWOWEEKS && !quietlog) {
  553.             ttp = localtime(&pwd->pw_change);
  554.             (void)printf("Warning: your password expires on %s %d, %d\n",
  555.                 months[ttp->tm_mon], ttp->tm_mday, TM_YEAR_BASE + ttp->tm_year);
  556.         }
  557.     if (pwd->pw_expire)
  558.         if (tp.tv_sec >= pwd->pw_expire) {
  559.             (void)printf("Sorry -- your account has expired.\n");
  560.             sleepexit(1);
  561.         }
  562.         else if (tp.tv_sec - pwd->pw_expire < TWOWEEKS && !quietlog) {
  563.             ttp = localtime(&pwd->pw_expire);
  564.             (void)printf("Warning: your account expires on %s %d, %d\n",
  565.                 months[ttp->tm_mon], ttp->tm_mday, TM_YEAR_BASE + ttp->tm_year);
  566.         }
  567.  
  568.     /* nothing else left to fail -- really log in */
  569.     {
  570.         struct utmp utmp;
  571.  
  572.         memset((char *)&utmp, 0, sizeof(utmp));
  573.         (void)time(&utmp.ut_time);
  574.         strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
  575.         if (hostname)
  576.             strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
  577.         strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
  578.         login(&utmp);
  579.     }
  580. #else
  581.     /* for linux, write entries in utmp and wtmp */
  582.     {
  583.         struct utmp ut;
  584.         char *ttyabbrev;
  585.         int wtmp;
  586.         
  587.         memset((char *)&ut, 0, sizeof(ut));
  588.         ut.ut_type = USER_PROCESS;
  589.         ut.ut_pid = getpid();
  590.         strncpy(ut.ut_line, ttyn + sizeof("/dev/")-1, sizeof(ut.ut_line));
  591.         ttyabbrev = ttyn + sizeof("/dev/tty") - 1;
  592.         strncpy(ut.ut_id, ttyabbrev, sizeof(ut.ut_id));
  593.         (void)time(&ut.ut_time);
  594.         strncpy(ut.ut_user, username, sizeof(ut.ut_user));
  595.         
  596.         /* fill in host and ip-addr fields when we get networking */
  597.         if (hostname) {
  598.             struct hostent *he;
  599.  
  600.             strncpy(ut.ut_host, hostname, sizeof(ut.ut_host));
  601.             if ((he = gethostbyname(hostname)))
  602.               memcpy(&ut.ut_addr, he->h_addr_list[0],
  603.                  sizeof(ut.ut_addr));
  604.         }
  605.         
  606.         utmpname(_PATH_UTMP);
  607.         setutent();
  608.         pututline(&ut);
  609.         endutent();
  610.         
  611.         if((wtmp = open(_PATH_WTMP, O_APPEND|O_WRONLY)) >= 0) {
  612.                 flock(wtmp, LOCK_EX);
  613.             write(wtmp, (char *)&ut, sizeof(ut));
  614.                 flock(wtmp, LOCK_UN);
  615.             close(wtmp);
  616.         }
  617.     }
  618.         /* fix_utmp_type_and_user(username, ttyn, LOGIN_PROCESS); */
  619. #endif
  620.  
  621.     dolastlog(quietlog);
  622.     
  623. #ifndef linux
  624.     if (!hflag) {                    /* XXX */
  625.         static struct winsize win = { 0, 0, 0, 0 };
  626.  
  627.         (void)ioctl(0, TIOCSWINSZ, &win);
  628.     }
  629. #endif
  630.     (void)chown(ttyn, pwd->pw_uid,
  631.         (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
  632.  
  633.     (void)chmod(ttyn, 0622);
  634.     (void)setgid(pwd->pw_gid);
  635.  
  636.     initgroups(username, pwd->pw_gid);
  637.  
  638. #ifdef HAVE_QUOTA
  639.     quota(Q_DOWARN, pwd->pw_uid, (dev_t)-1, 0);
  640. #endif
  641.  
  642.     if (*pwd->pw_shell == '\0')
  643.         pwd->pw_shell = _PATH_BSHELL;
  644. #ifndef linux
  645.     /* turn on new line discipline for the csh */
  646.     else if (!strcmp(pwd->pw_shell, _PATH_CSHELL)) {
  647.         ioctlval = NTTYDISC;
  648.         (void)ioctl(0, TIOCSETD, &ioctlval);
  649.     }
  650. #endif
  651.  
  652.     /* preserve TERM even without -p flag */
  653.     {
  654.         char *ep;
  655.         
  656.         if(!((ep = getenv("TERM")) && (termenv = strdup(ep))))
  657.           termenv = "dumb";
  658.     }
  659.  
  660.     /* destroy environment unless user has requested preservation */
  661.     if (!pflag)
  662.         {
  663.           environ = (char**)malloc(sizeof(char*));
  664.       memset(environ, 0, sizeof(char*));
  665.     }
  666.  
  667. #ifndef linux
  668.     (void)setenv("HOME", pwd->pw_dir, 1);
  669.     (void)setenv("SHELL", pwd->pw_shell, 1);
  670.     if (term[0] == '\0')
  671.         strncpy(term, stypeof(tty), sizeof(term));
  672.     (void)setenv("TERM", term, 0);
  673.     (void)setenv("USER", pwd->pw_name, 1);
  674.     (void)setenv("PATH", _PATH_DEFPATH, 0);
  675. #else
  676.         (void)setenv("HOME", pwd->pw_dir, 0);      /* legal to override */
  677.         if(pwd->pw_uid)
  678.           (void)setenv("PATH", _PATH_DEFPATH, 1);
  679.         else
  680.           (void)setenv("PATH", _PATH_DEFPATH_ROOT, 1);
  681.     (void)setenv("SHELL", pwd->pw_shell, 1);
  682.     (void)setenv("TERM", termenv, 1);
  683.  
  684.         /* mailx will give a funny error msg if you forget this one */
  685.         (void)sprintf(tmp,"%s/%s",_PATH_MAILDIR,pwd->pw_name);
  686.         (void)setenv("MAIL",tmp,0);
  687.  
  688.         /* LOGNAME is not documented in login(1) but
  689.        HP-UX 6.5 does it. We'll not allow modifying it.
  690.      */
  691.     (void)setenv("LOGNAME", pwd->pw_name, 1);
  692. #endif
  693.  
  694. #ifndef linux
  695.     if (tty[sizeof("tty")-1] == 'd')
  696.         syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
  697. #endif
  698.     if (pwd->pw_uid == 0)
  699.         if (hostname)
  700.             syslog(LOG_NOTICE, "ROOT LOGIN ON %s FROM %s",
  701.                 tty, hostname);
  702.         else
  703.             syslog(LOG_NOTICE, "ROOT LOGIN ON %s", tty);
  704.  
  705.     if (!quietlog) {
  706.         struct stat st;
  707.  
  708.         motd();
  709.         (void)sprintf(tbuf, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
  710.         if (stat(tbuf, &st) == 0 && st.st_size != 0)
  711.             (void)printf("You have %smail.\n",
  712.                 (st.st_mtime > st.st_atime) ? "new " : "");
  713.     }
  714.  
  715.     (void)signal(SIGALRM, SIG_DFL);
  716.     (void)signal(SIGQUIT, SIG_DFL);
  717.     (void)signal(SIGINT, SIG_DFL);
  718.     (void)signal(SIGTSTP, SIG_IGN);
  719.     (void)signal(SIGHUP, SIG_DFL);
  720.  
  721.     /* discard permissions last so can't get killed and drop core */
  722.     if(setuid(pwd->pw_uid) < 0 && pwd->pw_uid) {
  723.         syslog(LOG_ALERT, "setuid() failed");
  724.         exit(1);
  725.     }
  726.  
  727.     /* wait until here to change directory! */
  728.     if (chdir(pwd->pw_dir) < 0) {
  729.         (void)printf("No directory %s!\n", pwd->pw_dir);
  730.         if (chdir("/"))
  731.             exit(0);
  732.         pwd->pw_dir = "/";
  733.         (void)printf("Logging in with home = \"/\".\n");
  734.     }
  735.  
  736.     /* if the shell field has a space: treat it like a shell script */
  737.     if (strchr(pwd->pw_shell, ' ')) {
  738.         char *buff = malloc(strlen(pwd->pw_shell) + 6);
  739.         if (buff) {
  740.         strcpy(buff, "exec ");
  741.         strcat(buff, pwd->pw_shell);
  742.         execlp("/bin/sh", "-sh", "-c", buff, (char *)0);
  743.         fprintf(stderr, "login: couldn't exec shell script: %s.\n",
  744.             strerror(errno));
  745.         exit(0);
  746.         }
  747.         fprintf(stderr, "login: no memory for shell script.\n");
  748.         exit(0);
  749.     }
  750.  
  751.     tbuf[0] = '-';
  752.     strcpy(tbuf + 1, ((p = rindex(pwd->pw_shell, '/')) ?
  753.               p + 1 : pwd->pw_shell));
  754.  
  755.     execlp(pwd->pw_shell, tbuf, (char *)0);
  756.     (void)fprintf(stderr, "login: no shell: %s.\n", strerror(errno));
  757.     exit(0);
  758. }
  759.  
  760. void
  761. getloginname()
  762. {
  763.     register int ch;
  764.     register char *p;
  765.     static char nbuf[UT_NAMESIZE + 1];
  766.  
  767.     for (;;) {
  768.         (void)printf("\n%s login: ", thishost); fflush(stdout);
  769.         for (p = nbuf; (ch = getchar()) != '\n'; ) {
  770.             if (ch == EOF) {
  771.                 badlogin(username);
  772.                 exit(0);
  773.             }
  774.             if (p < nbuf + UT_NAMESIZE)
  775.                 *p++ = ch;
  776.         }
  777.         if (p > nbuf)
  778.             if (nbuf[0] == '-')
  779.                 (void)fprintf(stderr,
  780.                     "login names may not start with '-'.\n");
  781.             else {
  782.                 *p = '\0';
  783.                 username = nbuf;
  784.                 break;
  785.             }
  786.     }
  787. }
  788.  
  789. void timedout()
  790. {
  791.     struct termio ti;
  792.     
  793.     (void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
  794.  
  795.     /* reset echo */
  796.     (void) ioctl(0, TCGETA, &ti);
  797.     ti.c_lflag |= ECHO;
  798.     (void) ioctl(0, TCSETA, &ti);
  799.     exit(0);
  800. }
  801.  
  802. int
  803. rootterm(ttyn)
  804.     char *ttyn;
  805. #ifndef linux
  806. {
  807.     struct ttyent *t;
  808.  
  809.     return((t = getttynam(ttyn)) && t->ty_status&TTY_SECURE);
  810. }
  811. #else
  812.   int fd;
  813.   char buf[100],*p;
  814.   int cnt, more;
  815.  
  816.   fd = open(SECURETTY, O_RDONLY);
  817.   if(fd < 0) return 1;
  818.  
  819.   /* read each line in /etc/securetty, if a line matches our ttyline
  820.      then root is allowed to login on this tty, and we should return
  821.      true. */
  822.   for(;;) {
  823.     p = buf; cnt = 100;
  824.     while(--cnt >= 0 && (more = read(fd, p, 1)) == 1 && *p != '\n') p++;
  825.     if(more && *p == '\n') {
  826.         *p = '\0';
  827.           if(!strcmp(buf, ttyn)) {
  828.               close(fd);
  829.               return 1;
  830.           } else
  831.               continue;
  832.       } else {
  833.           close(fd);
  834.           return 0;
  835.       }
  836.   }
  837. }
  838. #endif
  839.  
  840. jmp_buf motdinterrupt;
  841.  
  842. void
  843. motd()
  844. {
  845.     register int fd, nchars;
  846.     void (*oldint)(), sigint();
  847.     char tbuf[8192];
  848.  
  849.     if ((fd = open(_PATH_MOTDFILE, O_RDONLY, 0)) < 0)
  850.         return;
  851.     oldint = signal(SIGINT, sigint);
  852.     if (setjmp(motdinterrupt) == 0)
  853.         while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
  854.             (void)write(fileno(stdout), tbuf, nchars);
  855.     (void)signal(SIGINT, oldint);
  856.     (void)close(fd);
  857. }
  858.  
  859. void sigint()
  860. {
  861.     longjmp(motdinterrupt, 1);
  862. }
  863.  
  864. void
  865. checknologin()
  866. {
  867.     register int fd, nchars;
  868.     char tbuf[8192];
  869.  
  870.     if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
  871.         while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
  872.             (void)write(fileno(stdout), tbuf, nchars);
  873.         sleepexit(0);
  874.     }
  875. }
  876.  
  877. void
  878. dolastlog(quiet)
  879.     int quiet;
  880. {
  881.     struct lastlog ll;
  882.     int fd;
  883.  
  884.     if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
  885.         (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
  886.         if (!quiet) {
  887.             if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
  888.                 ll.ll_time != 0) {
  889.                 (void)printf("Last login: %.*s ",
  890.                     24-5, (char *)ctime(&ll.ll_time));
  891.  
  892.                 if (*ll.ll_host != '\0')
  893.                   printf("from %.*s\n",
  894.                      (int)sizeof(ll.ll_host), ll.ll_host);
  895.                 else
  896.                   printf("on %.*s\n",
  897.                      (int)sizeof(ll.ll_line), ll.ll_line);
  898.             }
  899.             (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
  900.         }
  901.         memset((char *)&ll, 0, sizeof(ll));
  902.         (void)time(&ll.ll_time);
  903.         strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
  904.         if (hostname)
  905.             strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
  906.         (void)write(fd, (char *)&ll, sizeof(ll));
  907.         (void)close(fd);
  908.     }
  909. }
  910.  
  911. void
  912. badlogin(name)
  913.     char *name;
  914. {
  915.     if (failures == 0)
  916.         return;
  917.  
  918.     if (hostname)
  919.         syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s, %s",
  920.             failures, failures > 1 ? "S" : "", hostname, name);
  921.     else
  922.         syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s, %s",
  923.             failures, failures > 1 ? "S" : "", tty, name);
  924. }
  925.  
  926. #undef    UNKNOWN
  927. #define    UNKNOWN    "su"
  928.  
  929. #ifndef linux
  930. char *
  931. stypeof(ttyid)
  932.     char *ttyid;
  933. {
  934.     struct ttyent *t;
  935.  
  936.     return(ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
  937. }
  938. #endif 
  939.  
  940. void
  941. checktty(user, tty)
  942.      char *user;
  943.      char *tty;
  944. {
  945.     FILE *f;
  946.     char buf[256];
  947.     char *ptr;
  948.     char devname[50];
  949.     struct stat stb;
  950.  
  951.     /* no /etc/usertty, default to allow access */
  952.     if(!(f = fopen(_PATH_USERTTY, "r"))) return;
  953.  
  954.     while(fgets(buf, 255, f)) {
  955.  
  956.     /* strip comments */
  957.     for(ptr = buf; ptr < buf + 256; ptr++) 
  958.       if(*ptr == '#') *ptr = 0;
  959.  
  960.     strtok(buf, " \t");
  961.     if(strncmp(user, buf, 8) == 0) {
  962.         while((ptr = strtok(NULL, "\t\n "))) {
  963.         if(strncmp(tty, ptr, 10) == 0) {
  964.             fclose(f);
  965.             return;
  966.         }
  967.         if(strcmp("PTY", ptr) == 0) {
  968. #ifdef linux
  969.             sprintf(devname, "/dev/%s", ptr);
  970.             /* VERY linux dependent, recognize PTY as alias
  971.                for all pseudo tty's */
  972.             if((stat(devname, &stb) >= 0)
  973.                && major(stb.st_rdev) == 4 
  974.                && minor(stb.st_rdev) >= 192) {
  975.             fclose(f);
  976.             return;
  977.             }
  978. #endif
  979.         }
  980.         }
  981.         /* if we get here, /etc/usertty exists, there's a line
  982.            beginning with our username, but it doesn't contain the
  983.            name of the tty where the user is trying to log in.
  984.            So deny access! */
  985.         fclose(f);
  986.         printf("Login on %s denied.\n", tty);
  987.         badlogin(user);
  988.         sleepexit(1);
  989.     }
  990.     }
  991.     fclose(f);
  992.     /* users not mentioned in /etc/usertty are by default allowed access
  993.        on all tty's */
  994. }
  995.  
  996. void
  997. getstr(buf, cnt, err)
  998.     char *buf, *err;
  999.     int cnt;
  1000. {
  1001.     char ch;
  1002.  
  1003.     do {
  1004.         if (read(0, &ch, sizeof(ch)) != sizeof(ch))
  1005.             exit(1);
  1006.         if (--cnt < 0) {
  1007.             (void)fprintf(stderr, "%s too long\r\n", err);
  1008.             sleepexit(1);
  1009.         }
  1010.         *buf++ = ch;
  1011.     } while (ch);
  1012. }
  1013.  
  1014. void
  1015. sleepexit(eval)
  1016.     int eval;
  1017. {
  1018.     sleep((unsigned int)5);
  1019.     exit(eval);
  1020. }
  1021.  
  1022.